package com.kingbook.core.executor;

import com.kingbook.common.utils.StringUtils;
import com.kingbook.core.utils.JDBCHelper;
import com.google.common.collect.Maps;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Map;

/**
 * @Author: Bucky
 * @Date: 2019/1/2
 * @Version 1.0
 */
class MapResultSetExtractor implements ResultSetExtractor<Map<String, Object>> {


    @Override
    public Map<String, Object> extractData(ResultSet rs) throws SQLException {
        Map<String, Object> map = Maps.newHashMap();
        if (rs.next()) {
            ResultSetMetaData rsm = rs.getMetaData();
            int columnCount = rsm.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                map.put(StringUtils.toCamelCase(rsm.getColumnLabel(i)), JDBCHelper.getFormatResult(rs, i));
            }
        }
        return map;
    }
}